package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzZzsptfp;
import com.qixin.modules.rzInvoice.entity.EaRzZzsptfpItems;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsptfpItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzZzsptfpMapper;
import com.qixin.modules.rzInvoice.service.IEaRzZzsptfpService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 增值税普通发票 卷票
 * @Author: qixin-boot
 * @Date:   2021-07-08
 * @Version: V1.0
 */
@Service
public class EaRzZzsptfpServiceImpl extends ServiceImpl<EaRzZzsptfpMapper, EaRzZzsptfp> implements IEaRzZzsptfpService {

	@Autowired
	private EaRzZzsptfpMapper eaRzZzsptfpMapper;
	@Autowired
	private EaRzZzsptfpItemsMapper eaRzZzsptfpItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzZzsptfp eaRzZzsptfp, List<EaRzZzsptfpItems> eaRzZzsptfpItemsList) {
		eaRzZzsptfpMapper.insert(eaRzZzsptfp);
		if(eaRzZzsptfpItemsList!=null && eaRzZzsptfpItemsList.size()>0) {
			for(EaRzZzsptfpItems entity:eaRzZzsptfpItemsList) {
				//外键设置
				entity.setEaRzZzsptfpId(eaRzZzsptfp.getId());
				eaRzZzsptfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzZzsptfp eaRzZzsptfp,List<EaRzZzsptfpItems> eaRzZzsptfpItemsList) {
		eaRzZzsptfpMapper.updateById(eaRzZzsptfp);
		
		//1.先删除子表数据
		eaRzZzsptfpItemsMapper.deleteByMainId(eaRzZzsptfp.getId());
		
		//2.子表数据重新插入
		if(eaRzZzsptfpItemsList!=null && eaRzZzsptfpItemsList.size()>0) {
			for(EaRzZzsptfpItems entity:eaRzZzsptfpItemsList) {
				//外键设置
				entity.setEaRzZzsptfpId(eaRzZzsptfp.getId());
				eaRzZzsptfpItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzZzsptfpItemsMapper.deleteByMainId(id);
		eaRzZzsptfpMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzZzsptfpItemsMapper.deleteByMainId(id.toString());
			eaRzZzsptfpMapper.deleteById(id);
		}
	}
	
}
